package com.de.demo.client.service;

import com.de.demo.client.api.UserInfoApi;
import com.de.demo.client.dto.UserInfoDTO;
import com.de.demo.client.mapper.UserInfoMapper;
import com.de.demo.client.param.RegisterParam;
import com.de.demo.client.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class UserInfoService implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 登录校验
     * @param account
     * @param password
     * @return
     */
    public UserInfoDTO checkUserInfo(String account, String password){
        if(Objects.isNull(account) || Objects.isNull(password)){
            return null;
        }
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        UserInfo userInfo = userInfoMapper.checkUserInfo(account, password);
        if(Objects.isNull(userInfo)){
            log.info("账号不存在或者账号密码错误！！");
            userInfoDTO.setState(false);
            return userInfoDTO;
        }else {
            userInfoDTO.setState(true);
            userInfoDTO.setAccount(userInfo.getAccount());
            userInfoDTO.setName(userInfo.getName());
            return userInfoDTO;
        }
    }

    @Override
    public Boolean register(RegisterParam registerParam) {
        UserInfo userInfo = new UserInfo();
        Date date = new Date();
        userInfo.setCreateTime(date);
        userInfo.setModifyTime(date);
        userInfo.setAccount(registerParam.getAccount());
        userInfo.setAge(registerParam.getAge());
        userInfo.setName(registerParam.getName());
        userInfo.setPhone(registerParam.getPhone());
        userInfo.setPassword(registerParam.getPassword());
        int count = userInfoMapper.insertSelective(userInfo);
        if(count > 0){
            return true;
        }else {
            return false;
        }
    }
}
